package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

/**
 * 打卡活动数据传输对象
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "打卡活动信息")
public class CheckInActivityDTO {

    @NotNull(message = "活动ID不能为空")
    @Schema(description = "活动唯一标识", example = "1")
    private Long id;

    @NotBlank(message = "活动标题不能为空")
    @Size(max = 200, message = "活动标题长度不能超过200个字符")
    @Schema(description = "活动标题", example = "2024年春季科技创新营")
    private String title;

    @NotNull(message = "开始日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动开始日期", example = "2024-03-01")
    private LocalDate startDate;

    @NotNull(message = "结束日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动结束日期", example = "2024-03-07")
    private LocalDate endDate;

    @NotNull(message = "打卡状态不能为空")
    @Schema(description = "今日是否已打卡", example = "true")
    private Boolean checked;

    @Min(value = 1, message = "活动天数至少为1天")
    @Schema(description = "活动总天数", example = "7")
    private Integer totalDays;

    @Min(value = 0, message = "已打卡天数不能为负数")
    @Schema(description = "已打卡天数", example = "3")
    private Integer checkedDays;

    /**
     * 获取打卡进度百分比
     *
     * @return 打卡进度(0-100)
     */
    @Schema(description = "打卡进度百分比", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getProgressPercentage() {
        if (totalDays == null || totalDays == 0 || checkedDays == null) {
            return 0;
        }
        return Math.min(100, (checkedDays * 100) / totalDays);
    }

    /**
     * 获取剩余打卡天数
     *
     * @return 剩余打卡天数
     */
    @Schema(description = "剩余打卡天数", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getRemainingDays() {
        if (totalDays == null || checkedDays == null) {
            return 0;
        }
        return Math.max(0, totalDays - checkedDays);
    }
}
